﻿/// <reference path="tfcore.js" />
/// <reference path="appcommon.js" />
// #pragma NoCompStart
/*
tfpopup.js  2.0a
handles modal dialog boxes, etc
*/
// #pragma NoCompEnd
// ModalDlg code

//NameSpaces defined
_TF = top;
_TF.Controls = _TF.Controls || {};
_TF.Controls.Dialog = _TF.Controls.Dialog || {};

var returnFunc = { pbContentPage: 1, pbPrevDlg: 2 };  // cause postback of IFrame, or Postback of Previous Modal Dialog, OR send a javascript function to run after button clicked.
function getWidth(width) { var pWidth; if (width == undefined) { width = '85%'; } if (isNaN(width)) { if (width.search(/\%/) != -1) { pWidth = getViewPortWidth() * (width.replace("\%", "") / 100); } } else { pWidth = width; } return pWidth; };
function getHeight(height) { var pHeight; if (height == undefined) { height = '90%'; } if (isNaN(height)) { if (height.search(/\%/) != -1) { pHeight = getViewPortHeight() * (height.replace("\%", "") / 100); } } else { pHeight = height; } return pHeight; };
function getScrollTop() { if (top.pageYOffset) { return top.pageYOffset; } else if (top.document.documentElement && top.document.documentElement.scrollTop) { return top.document.documentElement.scrollTop; } else if (top.document.body) { return top.document.body.scrollTop; } };


var DlgTabIndexes = [];
var DlgDefaultpage = "about:blank";

_TF.Controls.Dialog.Iteration = -1;

// Pre-defined list of tags we want to disable/enable tabbing into
var DlgTabbableTags = ["A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME"];
function disableTabIndexes() { if (document.all) { var i = 0; for (var j = 0; j < DlgTabbableTags.length; j++) { var tagElements = document.getElementsByTagName(DlgTabbableTags[j]); for (var k = 0; k < tagElements.length; k++) { DlgTabIndexes[i] = tagElements[k].tabIndex; tagElements[k].tabIndex = "-1"; i++; } } } };
function restoreTabIndexes() { if (document.all) { var i = 0; for (var j = 0; j < DlgTabbableTags.length; j++) { var tagElements = document.getElementsByTagName(DlgTabbableTags[j]); for (var k = 0; k < tagElements.length; k++) { tagElements[k].tabIndex = DlgTabIndexes[i]; tagElements[k].tabEnabled = true; i++; } } } };

function _Dialog(options) {
    /// <summary>Instantiates a new ModalDlg as an alert an iframe or a MessageBox</summary>
    /// <param name="options" type="object">
    ///  Options = {
    ///    oName:'', The Object Name to clear when done.
    ///    id: '',
    ///    type: [ iframe=1, html=2, alert=3, MsgBox=4, csMsgBox=5 ],
    ///    width: 600,
    ///    height: 600,
    ///    loc: '[CEN, N, NE, NW, W, SW, S, SE, E]',
    ///    title: 'Title of Dialog',
    ///    alertText: 'Alert Text',
    ///    alertType: [None=0,Error=1, Warning=2, Information=3],
    ///    alertTask: 'current task',
    ///    msgboxType: [None=0,ok=1, yes=2, yesno=3, yesnocancel=4],
    ///    msgboxQuestion: '',
    ///    URL: '',
    ///    HTML: '',
    ///    returnFocusID: '',
    ///    returnFunc: '',
    ///    showClose: false,
    ///    autoClose: -1,
    ///    modal: false
    ///}
    /// </param>
    /// <param name="height">width of the dialog</param>
    /// <param name="loc">use the locTypes to see what is available (locTypes.N = top of screen)</param>
    /// <returns>instance of the messagebox</returns>

    //Set up default options:
    var defaultOptions = {
        oName: '',
        id: '',
        type: _TF.APP.Enum.contentTypes.html,
        width: 600,
        height: 600,
        loc: 'CEN',
        title: 'Dialog',
        alertText: _TF.APP.Global.g_jsErrorTxt,
        alertType: 0,
        alertTask: 'unknown',
        msgboxType: 0,
        msgboxQuestion: '',
        URL: '',
        HTML: '',
        returnFocusID: '',
        returnFunc: '',
        showClose: true,
        autoClose: -1,
        modal: false
    };

    options = options || {};
    for (var name in defaultOptions) {
        if (!options.hasOwnProperty(name)) {
            options[name] = defaultOptions[name];
        }
    }

    _TF.Controls.Dialog.Iteration++;

    var _me = this; // Instantiated Object = "_me"
    _me.iter = _TF.Controls.Dialog.Iteration;

    _me.options = options;
    //    _me.options.id = options.id;
    //    _me.contentType = options.type;
    //    _me.width = options.width;
    //    _me.msgboxQuestion = options.msgboxQuestion;
    //    _me.msgboxType = options.msgboxType;
    //    _me.height = options.height;
    //    _me.loc = options.loc;
    //    _me.title = options.title;
    //    _me.options.showClose = options.showClose;
    //    _me.alertText = options.alertText;
    //    _me.alertType = options.alertType;
    //    _me.autoClose = options.autoClose;
    //    _me.setFocusId = options.returnFocusID;
    //    _me.callBackFunc = options.returnFunc;
    //    _me.myHTML = options.HTML;
    //    _me.myURL = options.URL;
    //    _me.sTask = options.alertTask;
    //    _me.isModal = options.modal;
    //    _me.oName = options.oName;

    var strType = "mdl";
    if (_me.options.type != _TF.APP.Enum.contentTypes.iframe) { strType = "alt"; }

    _me.tBody = document.getElementById(strType + 'Dialog' + _me.iter);
    if (_me.tBody == null) {
        _me.tBody = document.createElement("div");
        _me.tBody.id = strType + "Dialog" + _me.iter;
        document.body.appendChild(_me.tBody);
    }
    _me.Mask = document.createElement("div"); _me.Mask.id = strType + "DlgMask_" + _me.iter; _me.Mask.className = "ModalDlgMask";
    _me.Con = document.createElement("div"); _me.Con.id = strType + "DlgContainer_" + _me.iter; _me.Con.className = "ModalDlgContainer";
    _me.MDI = document.createElement("div");
    _me.MDTB = document.createElement("div");
    _me.MDT = document.createElement("div");
    _me.MDC = document.createElement("div");
    _me.HFLD = document.createElement("INPUT");
    _me.Content = null;
    _me.arrTemp = [];


    // private functions
    this.setTask = function(sTask) { _me.options.alertTask = sTask; };
    this.setType = function(contentType) { _me.options.type = contentType; };
    this.setURL = function(sUrl) { _me.options.URL = sUrl; };
    this.setCallBackFunc = function(callBackFunc) { _me.options.returnFunc = callBackFunc; };
    this.setFocusOnReturn = function(elId) { _me.options.returnFocusID = elId; };
    this.setHTML = function(newHTML) { _me.options.HTML = newHTML; };
    this.setModal = function(sModal) { _me.options.modal = sModal; };
    this.setAlertType = function(aType) { _me.options.alertType = aType; };
    this.setTitle = function(sTitle) { _me.options.title = sTitle; };
    this.setLocation = function(locType) { _me.options.loc = locType; };
    this.setAutoClose = function(sSeconds) { _me.options.autoClose = sSeconds * 1000; };

    this.init = function() {
        var arrTemp = [];
        _me.MDI.id = "MDI" + _me.iter;
        _me.MDTB.id = "MDTB" + _me.iter;
        _me.MDT.id = "MDT" + _me.iter;
        _me.MDC.id = "MDC" + _me.iter;
        _me.HFLD.id = "HFLD" + _me.iter;
        _me.HFLD.setAttribute("type", "button");

        if (_me.options.showClose) {
            var cImg = document.createElement("img");
            cImg.id = "popCloseBox_" + _me.iter;
        }

        var image;
        switch (_me.options.type) {
            case _TF.APP.Enum.contentTypes.MsgBox: case _TF.APP.Enum.contentTypes.csMsgBox:
                image = _TF.ControlDir + "images/information.jpg";
                var cs = _me.options.type == _TF.APP.Enum.contentTypes.csMsgBox ? true : false;
                var onClose = "javascript:_RaiseEvent('" + _me.options.id + "','OnClose','~t');";
                var strButtons = '';
                if (_me.options.id == '') { _me.options.id = 'msgBoxValue'; }

                if (_me.options.returnFunc == returnFunc.pbContentPage) {
                    onClose += "frames['ifMainContent'].__doPostBack('" + _me.options.id + "','~t');";
                }
                else if (_me.options.returnFunc == returnFunc.pbPrevDlg) {
                    onClose += "javascript:_RaiseEvent('" + _me.options.id + "','OnClose','~t');postBackbPrevDlg('" + _me.options.id + "','~t');";
                }

                _me.options.returnFunc = ""; // clear out return function as we already raisevent!
                onClose += "_TF.APP.Global.P_MsgBox.hide(" + _me.options.type + ");";
                image = _TF.ControlDir + "images/sminformation.jpg";
                switch (_me.options.msgboxType) {
                    case _TF.APP.Enum.msgboxType.ok:
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "ok") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td style='width:50%;'>&nbsp;</td>";
                        break;
                    case _TF.APP.Enum.msgboxType.yes:
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                        strButtons += "<td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                    case _TF.APP.Enum.msgboxType.yesno:
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                        strButtons += "<td>&nbsp;&nbsp;&nbsp;</td>";
                        strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                    case _TF.APP.Enum.msgboxType.yesnocancel:
                        strButtons += "<td style='width:50%;'></td>";
                        strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                        strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                        strButtons += "<td><input id='s3' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "cancel") + "\" type=button value=Cancel name=MessageBoxIDCancel1 /></td></td>";
                        strButtons += "<td style='width:50%;'></td>";
                        break;
                }

                if (_me.options.showClose) { cImg.setAttribute("src", _TF.ControlDir + "images/close.gif"); }
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.options.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                var strHTML = "<table style='width: 100%;text-align:center;background-color: lightgrey;'>";
                strHTML += "<tr><td ><img style='border: black 1px solid; height:25px' src='" + image + "' alt='' /></td>";
                strHTML += "<td style='text-align:left;padding:5px;' colspan='4'>" + _me.options.msgboxQuestion + "</td></tr><tr>";
                strHTML += strButtons;
                strHTML += "</tr></table></div></div>";
                _me.Content.innerHTML = strHTML;
                _me.setModal(true);
                strButtons = '';
                break;

            case _TF.APP.Enum.contentTypes.html:
                image = _TF.ControlDir + "images/sminformation.jpg";
                if (_me.options.showClose) { cImg.setAttribute("src", _TF.ControlDir + "images/smclose.gif"); }
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.options.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                _me.Content.innerHTML = _me.options.HTML;
                _me.setModal(_me.options.modal);
                //"<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.errText + "</td></tr></table></div></div>";
                break;
            case _TF.APP.Enum.contentTypes.alert:
                image = _TF.ControlDir + "images/sminformation.jpg";
                switch (_me.options.alertType) {
                    case _TF.APP.Enum.alertTypes.Error:
                        _me.options.autoClose = -1;
                        //edNLog.error(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                        image = _TF.ControlDir + "images/smerror.jpg";
                        break;
                    case _TF.APP.Enum.alertTypes.Warning:
                        //edNLog.warning(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                        image = _TF.ControlDir + "images/smwarning.jpg";
                        break;
                    case _TF.APP.Enum.alertTypes.Information:
                        //edNLog.info(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                        image = _TF.ControlDir + "images/sminformation.jpg";
                        break;
                }
                if (_me.options.showClose) { cImg.setAttribute("src", _TF.ControlDir + "images/smclose.gif"); }
                _me.Content = document.createElement("div");
                _me.MDI.className = "NotifyDlgInner";
                _me.MDTB.className = "NotifyDlgTitleBar";
                _me.MDT.className = "NotifyDlgTitle";
                _me.MDT.innerHTML = _me.options.title;
                _me.MDC.className = "NotifyDlgControls";
                _me.Content.setAttribute("style", "overflow:auto;");
                _me.Content.innerHTML = "<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.options.alertText + "</td></tr></table></div></div>";
                break;
            case _TF.APP.Enum.contentTypes.iframe:
                _me.MDI.className = "ModalDlgInner";
                _me.MDTB.className = "ModalDlgTitleBar";
                _me.MDT.className = "ModalDlgTitle";
                _me.MDT.innerHTML = "<img src='" + _TF.ControlDir + "images/appicon.gif' alt=''></img>&nbsp;&nbsp;" + _me.options.title;
                _me.MDC.className = "ModalDlgControls";
                if (_me.options.showClose) { cImg.setAttribute("src", _TF.ControlDir + "images/close.gif"); }
                _me.Content = document.createElement("iframe");
                _me.Content.setAttribute("src", _me.options.URL);
                _me.Content.setAttribute("style", "width:100%;height:100%;background-color:transparent;");
                _me.Content.setAttribute("scrolling", "auto");
                _me.Content.setAttribute("frameborder", "0");
                _me.Content.setAttribute("allowtransparency", "true");
                _me.Content.className = "ModalDlgFrame";
                if (_me.options.id != '') {
                    _me.Content.id = _me.options.id;
                    _me.Content.setAttribute("name", _me.options.id);
                }
                else {
                    _me.Content.id = strType + "DlgFrame_" + _me.iter;
                    _me.Content.setAttribute("name", strType + "DlgFrame_" + _me.iter);
                }

                _me.Content.setAttribute("width", "100%");
                _me.Content.setAttribute("height", "100%");
                break;
            default: break;
        }

        if (_me.options.showClose) { _me.MDC.appendChild(cImg); }
        _me.MDTB.appendChild(_me.MDT);
        _me.MDTB.appendChild(_me.MDC);
        _me.MDI.appendChild(_me.MDTB);
        _me.MDI.appendChild(_me.Content);
        _me.Con.appendChild(_me.MDI);
        _me.tBody.appendChild(_me.Mask);
        _me.tBody.appendChild(_me.Con);
        //_TF.Element.EnableDrag(_me.MDTB);
        Drag.init(_me.Con);

        if (_me.options.showClose) { _AddEvent(_me.MDC, "click", function() { _me.hide(_me.options.type); }); }
    };

    this.show = function show() {
        var pWidth = getWidth(_me.options.width);
        var pHeight = getHeight(_me.options.height) - 30;

        this.init();
        var t = getLocationPoint(pWidth, pHeight, _me.options.loc);
        _me.Con.style.left = t.x;
        _me.Con.style.top = t.y;
        if (_me.options.alertType > 2) { _me.options.autoClose = 4000; }

        if (_me.options.modal) {
            //disableTabIndexes();
            _me.Mask.style.height = getViewPortHeight();
            _me.Mask.style.width = getViewPortWidth();
            _me.Mask.style.zIndex = 800 + (_me.iter * 3);
            _me.Mask.style.display = "block";
        }

        var titleBarHeight = parseInt(_me.MDTB.offsetHeight, 10);
        _me.Con.style.width = pWidth + "px";
        _me.Con.style.height = (pHeight + titleBarHeight) + "px";
        _me.Con.style.zIndex = 801 + (_me.iter * 3);
        _me.Con.style.display = "block";
        if (_me.options.autoClose > 0) { setTimeout(_me.hide, _me.options.autoClose); }

    };      // end show

    this.destroy = function() {
        _RemoveEvent(_me.MDC, "click", function() { _me.hide(_me.options.type); });
        _TF.Element.EnableDrag(_me.Con.id, '', '', '', false);
        _TF.Element.SafelyRemoveNode(_me.tBody); // Remove ALL children elements first to avoid memory leaks
        _TF.Controls.Dialog.Iteration--;
        if (_TF.Controls.Dialog.Iteration == -1) _TF.Controls.Dialog.Iteration = 0;
        _me = null;
        //_TF.DestroyObject(_me);
    };

    this.hide = function(iType) {
        if (_me != null) {
            if (_me.options.type == iType) {
                _me.Con.style.display = 'none';
                var fn = "";
                if (_me.options.returnFunc != null) { fn = _me.options.returnFunc; }
                if (_me.options.returnFocusID != null && _me.options.returnFocusID.length > 0) { fn += ";setFocus(" + _me.options.returnFocusID + ");"; }
                if (fn.length > 0) { setTimeout(fn, 10); }
                if (_me.options.modal) {
                    //    restoreTabIndexes();
                    if (_me.Mask != undefined) { _me.Mask.style.display = 'none'; }
                }
                setTimeout(this.destroy, 1000);
            }
        }
    };
};


function getLocationPoint(width, height, loc) {
    var theBody = document.getElementsByTagName("BODY")[0];
    var scTop = parseInt(getScrollTop(), 10);
    var scLeft = parseInt(theBody.scrollLeft, 10);
    var titleBarHeight = 0; // parseInt(_me.MDTB.offsetHeight, 10);
    var fullHeight = getViewPortHeight();
    var fullWidth = getViewPortWidth();
    var x, y;
    switch (loc) {
        case _TF.APP.Enum.locTypes.NW:
            x = "2px";
            y = "2px";
            break;
        case _TF.APP.Enum.locTypes.N:
            x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
            y = "2px";
            break;
        case _TF.APP.Enum.locTypes.NE:
            x = (getViewPortWidth() - width) + "px";
            y = "2px";
            break;
        case _TF.APP.Enum.locTypes.E:
            x = (getViewPortWidth() - width) + "px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + (10) + "px"
            break;
        case _TF.APP.Enum.locTypes.SE:
            break;
        case _TF.APP.Enum.locTypes.S:
            break;
        case _TF.APP.Enum.locTypes.SW:
            break;
        case _TF.APP.Enum.locTypes.W:
            x = "2px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
            break;
        case _TF.APP.Enum.locTypes.CEN:
            x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
            y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
            break;
        default: break;
    }
    return { x: x, y: y };
};


_TF.Controls.Dialog.postBackbPrevDlg = function(eventTarget, eventArg) {
    var el = xGetElementById("mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1));
    if (el != null) {
        frames["mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1)].__doPostBack(eventTarget, eventArg);
    }
};

_TF.Controls.Dialog.refreshPrevDlg = function(dlgId) {
    var el;
    if (dlgId != null) {
        el = _gE(dlgId);
    }
    else {
        el = xGetElementById("mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1));
    }

    el.src = el.src;
    return;
};

_TF.Controls.Dialog.getPrevDlg = function(dlgId) {
    var el;
    if (dlgId != null) {
        el = frames[dlgId].document.parentWindow;
    }
    else {
        el = frames["mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1)].document.parentWindow;
    }

    return el;
};

